<?php

/**
 * This is the model class for table "{{albums}}".
 *
 * The followings are the available columns in table '{{albums}}':
 * @property integer $zal_id
 * @property integer $zm_id
 * @property integer $zpc_id
 * @property integer $zat_id
 * @property string $zal_title
 * @property string $zal_desc
 * @property integer $inputtime
 * @property integer $updatetime
 */
class Albums extends CActiveRecord
{
	//时间排序
	public $listorder;
	public $albumid;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Albums the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{albums}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id, zpc_id, zat_id, inputtime, updatetime, albumid,listorder', 'numerical', 'integerOnly'=>true),
			array('zal_title', 'length', 'max'=>100),
			array('zal_desc', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zal_id, zm_id, zpc_id, zat_id, zal_title, zal_desc, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'photo'=>array(self::BELONGS_TO, 'Attachment', 'zat_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zal_id' => 'Zal',
			'zm_id' => 'Zm',
			'zpc_id' => 'Zpc',
			'zat_id' => 'Zat',
			'zal_title' => 'Zal Title',
			'zal_desc' => 'Zal Desc',
			'inputtime' => 'Inputtime',
			'updatetime' => 'Updatetime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zal_id',$this->zal_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zpc_id',$this->zpc_id);
		$criteria->compare('zat_id',$this->zat_id);
		$criteria->compare('zal_title',$this->zal_title,true);
		$criteria->compare('zal_desc',$this->zal_desc,true);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);
		
		if($this->listorder==2){
			$criteria->order = 'inputtime ASC';
		}else{
			$criteria->order = 'inputtime DESC';
		}

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>12,	
				),
		));
	}
	
	/**
	 * 查找前的操作
	 * 当前商家的所有末归类图片全部纳入未分类相册
	 */
	protected function beforeFind(){
		//当前商家会员
		$cmpid = Company::model()->getCmpUserId();
		//如果有商家用户登录
		if(!empty($cmpid)){
			//查看当前商家未归类图片
			$criteria=new CDbCriteria;
			$criteria->compare('zm_id',$cmpid);
			$criteria->compare('zat_image',2);//类型：2图片
			$criteria->compare('zat_status',1);//状态：1有效
			$criteria->addCondition(" zat_id NOT IN ( SELECT zat_id FROM {{albums}} WHERE zm_id=$cmpid ) ");
			$attachments = Attachment::model()->findAll($criteria);
			
			foreach($attachments as $val){
				$albumlog = new Albums();
				$albumlog->zm_id = $cmpid;
				$albumlog->zat_id = $val->zat_id;
				$albumlog->zpc_id = 0;//自定义分类ID:0末分类
				$albumlog->save();
			}
		}
		return true;
	}
	
	/**
	 * 保存前的操作
	 */
	protected function beforeSave(){
		if($this->isNewRecord){
			$this->inputtime = time();
		}else{
			$this->updatetime = time();
		}
		return true;
	}
	
	/**
	 * 时间排序方法，用于筛选
	 */
	public function getListOrder(){
		return array(
				''=>'按时间排序',
				'1'=>'时间降序',
				'2'=>'时间升序',
				);
	}
}